package us.webone.webone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import us.webone.webone.entity.Account;
import us.webone.webone.entity.AccountRole;
import us.webone.webone.service.IAccountRoleService;
import us.webone.webone.service.IAccountService;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author none
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/api/account")
public class AccountController {

    IAccountService accountService;

    IAccountRoleService accountRoleService;

    /**
     * 登录
     */
    @PostMapping("login")
    public ResponseEntity<String> login(@RequestBody Map<String, String> map, HttpSession session) {
        String username = map.get("username");
        String password = map.get("password");

        if (username == null || password == null) {
            return new ResponseEntity<>("账号密码不能为空", HttpStatus.BAD_REQUEST);
        }

        String token = DigestUtils.md5DigestAsHex(password.getBytes());

        Account account = getAccount(username);

        if (account == null) {
            return new ResponseEntity<>("账号不存在", HttpStatus.NOT_FOUND);
        } else if (token.equals(account.getToken())) {
            String key = String.valueOf(System.currentTimeMillis());
            session.setAttribute(String.valueOf(account.getId()), key);
            return new ResponseEntity<>("登录成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("未知错误", HttpStatus.BAD_REQUEST);
        }
    }

    /*获取账户*/
    private Account getAccount(String username) {
        QueryWrapper<Account> query = new QueryWrapper<>();
        query.eq("username", username);
        return accountService.getOne(query);
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public ResponseEntity<String> logout(HttpSession session, @CookieValue("id") String id) {
        if (session.getAttribute(id) != null) {
            session.removeAttribute(id);
            return new ResponseEntity<>("登出成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("尚未登录", HttpStatus.BAD_REQUEST);
        }

    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");

        if (username == null || password == null) {
            return new ResponseEntity<>("账号密码不能为空", HttpStatus.BAD_REQUEST);
        }

        Account account = getAccount(username);

        if (account != null) {
            return new ResponseEntity<>("账户已存在", HttpStatus.BAD_REQUEST);
        }

        String token = DigestUtils.md5DigestAsHex(password.getBytes());

        Account newAccount = new Account();
        newAccount.setUsername(username);
        newAccount.setToken(token);
        accountService.save(newAccount);

        AccountRole newAccountRole = new AccountRole();
        QueryWrapper<Account> query = new QueryWrapper<>();
        query.eq("username",username);
        newAccountRole.setAccountId(accountService.getOne(query).getId());
        //默认1为普通用户角色
        newAccountRole.setRoleId((long)1);
        accountRoleService.save(newAccountRole);

        return new ResponseEntity<>("账户注册成功", HttpStatus.OK);
    }

    /**
     * 验证username是否重复
     */
    @GetMapping("/username")
    public ResponseEntity<String> checkUsername(@RequestParam("username") String username) {
        Account account = getAccount(username);
        if (account != null) {
            return new ResponseEntity<>("账户名已已被使用", HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>("账户名未被使用", HttpStatus.OK);
        }
    }


}
